package logic

import (
	"context"
	"douban/doubanrpc/doubanrpc"
	"douban/doubanrpc/internal/svc"
	"douban/models/mysql"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *doubanrpc.ProductAddRequest) (*doubanrpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{

		Image: in.Image,
		Name:  in.Name,
		Price: float64(in.Price),
		Stock: in.Stock,
	}
	err := mysql.DB.Debug().Create(&product).Error
	if err != nil {
		return nil, fmt.Errorf("商品添加失败")
	}
	return &doubanrpc.ProductAddResponse{
		ProductId: int64(product.ID),
	}, nil
}
